#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>

#define STEP 4096

void mycat(int fd)
{
  char buf[STEP];
  int r = 0; 
  while ((r = read(fd, buf, STEP)) )
    {
      if (r == -1 && (errno == EINTR || errno == EAGAIN))
	continue;
      if(r == -1)
	break;
      write(STDOUT_FILENO, buf, r);
    }
  if (r == -1)
    {
      perror("read");
    }
}

int main(int argc, char** argv)
{
  int i;
  int fd;
  if (argc < 2)
    {
      write(STDERR_FILENO,"cat: missing parameters.\n",25);
      exit(1);
    }
  for (i=1; i < argc; ++i)
    {
      if (argv[i][0] == '-' && !argv[i][1])
	{
	  mycat(STDIN_FILENO);
	}
      else 
	{
	  fd = open(argv[i], O_RDONLY);
	  if(fd < 0)
	    {
	      perror("open");
	    }
	  mycat(fd);
	  close(fd);
	}
    }
  return errno;
}
